import wx from "weixin-js-sdk";
import { mapState } from "vuex";
import routes from '../../routers'; // 引入路由配置
import store from '../../vuex/store';
import Vue from "vue";

var shareLink = ''
var shareimgUrl = '';
var sharetitle = '';//"暧昧雪梨";
var shareDesc = '';
var urlLink = '';

var tokens = "";
var callBack=null;

export default{

      testFun:function(){
        console.log("link="+shareLink);
      },
      defultShareMsg:function(){
        shareLink = store.state.domainUrl+'login.html?type=into&storeid='+store.state.storeid;
        shareimgUrl = store.state.soursePath+store.state.storeInfo.shareicon;
        sharetitle = store.state.storeInfo.name;
        // shareimgUrl = store.state.shareImg;
        // sharetitle = store.state.storeName;
        shareDesc = '爱美丽找爱梨精选';
        // shareimgUrl = 'https://www.jubenxiong.com/games/dianwu/admin/public/upload/logo_18/2018/07-13/97eb27782cc0917fef3e87d0b6dd4f7c.png';
        // sharetitle = '爱梨跨商户测试店';
        // shareDesc = '爱美丽找爱梨精选';
      },
      updateShareMsg:function(slink,simg,sdesc,stitle){
        //更改分享信息
        if(slink!=''){
            shareLink = slink;
        }

        if(simg!=''){
            shareimgUrl = simg;
        }

        if(sdesc!=''){
            shareDesc = sdesc;
        }

        if(stitle!=''){
            sharetitle = stitle;
        }
        this.getWxData();
      },
      getWxData:function(callBack=null) {
        this.callBack=callBack;
        if(store.state.sistem=='ios'){
          urlLink=store.state.iosPath.split('#')[0];
        }else{
          urlLink = window.location.href;
        } 
        
        Vue.prototype.$http
          .post(store.state.shareUrl+"wxapi/share.php", {
            urls: encodeURIComponent(urlLink)
          })
          .then(response => {
            let data = response.body;
            var wxob = {};
  
            wxob.timestamp = data.timestamp;
            wxob.nonceStr = data.nonceStr;
            wxob.signature = data.signature;
            wxob.link = urlLink;
            this.setData(wxob, this);
            if(this.callBack) this.callBack();
          })
          .catch(function(response) {
            console.log(response);
          });
      },
      setData:function(obj, selfobj) {
        // console.log("this.shareimgUrl=" + selfobj.shareimgUrl);
        wx.config({
          debug: false,
          appId: store.state.wxappid,
          timestamp: obj.timestamp,
          nonceStr: obj.nonceStr,
          signature: obj.signature,
          jsApiList: [
            // 所有要调用的 API 都要加到这个列表中
            "onMenuShareTimeline",
            "onMenuShareAppMessage",
            "scanQRCode"
          ]
        });
        // if(store.state.sistem=='ios'){
        //   alert('ios 签名url='+urlLink);
        // }
        wx.ready(function() {
            console.log("shareimgUrl"+shareimgUrl);
          // 在这里调用 API
          wx.onMenuShareTimeline({
            title: sharetitle, // 分享标题
            
            //desc: shareDesc, // 分享描述
  
            link: shareLink,//selfobj.shareLink, // 分享链接
  
            imgUrl: shareimgUrl, // 分享图标
  
            success: function() {
              // 用户确认分享后执行的回调函数
              //console.log("im ok");
            },
  
            cancel: function() {
              // 用户取消分享后执行的回调函数
              //console.log("im fail");
            }
          });
  
          wx.onMenuShareAppMessage({
            title: sharetitle, // 分享标题
  
            desc: shareDesc, // 分享描述
  
            link: shareLink,//selfobj.shareLink, // 分享链接
  
            imgUrl: shareimgUrl, // 分享图标
  
            type: "", // 分享类型,music、video或link，不填默认为link
  
            dataUrl: "", // 如果type是music或video，则要提供数据链接，默认为空
  
            success: function() {
              // 用户确认分享后执行的回调函数
            },
  
            cancel: function() {
              // 用户取消分享后执行的回调函数
            }
          });
  
        });
  
        wx.error(function(res) {
          //console.log("res="+res.errMsg);
          // alert("error=" + res.errMsg);
        });
      }
}